﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NHibernate;
using BrokerBase.Lib.Company;
using BrokerBase.Lib.Core;

namespace Broker.View.CompanyView
{
    /// <summary>
    /// CompanyForm.xaml 的交互逻辑
    /// </summary>
    public partial class CompanyForm
    {

        private ICompany cmp;

        public CompanyForm(ICompany cmp)
        {
            InitializeComponent();
            this.cmp = cmp;
            Loaded += new RoutedEventHandler(CompanyForm_Loaded);
        }

        void CompanyForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = cmp;
            SetOperRight();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            try
            {
                if (!VerifyCompany())
                    return;

                IBaseService<Company> svr = new BaseService<Company>(LibGlobal.HbmSessionFactory.GetCurrentSession());

                svr.SaveOrUpdate(cmp as Company);
                MessageBox.Show("保存成功", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SetOperRight()
        {
            btnSave.Visibility = LibGlobal.AppType == AppType.Manager ? Visibility.Visible : Visibility.Collapsed;
            txtDescription.IsReadOnly = LibGlobal.AppType == AppType.Business;
            txtFullName.IsReadOnly = LibGlobal.AppType == AppType.Business;
            txtName.IsReadOnly = LibGlobal.AppType == AppType.Business;
            txtNumber.IsReadOnly = LibGlobal.AppType == AppType.Business;
            txtPhone.IsReadOnly = LibGlobal.AppType == AppType.Business;
            txtHomePage.IsReadOnly = LibGlobal.AppType == AppType.Business;
        }

        private bool VerifyCompany()
        {
            ISession sess = null;
            try
            {
                if (string.IsNullOrEmpty(cmp.FullName))
                {
                    MessageBox.Show("公司全名不能为空!");
                    return false;
                }

                if (string.IsNullOrEmpty(cmp.Name))
                {
                    MessageBox.Show("公司简称不能为空!");
                    return false;
                }

                if (string.IsNullOrEmpty(cmp.Number))
                {
                    MessageBox.Show("公司代码不能为空!");
                    return false;
                }

                if (cmp.Id == 0)
                {
                    sess = LibGlobal.HbmSessionFactory.GetCurrentSession();
                    IList<string> pName = null;

                    pName = sess.CreateQuery("select FullName from Company as p where p.FullName='" + cmp.FullName.Trim() + "'").List<string>();
                    if (pName.Count > 0)
                    {
                        MessageBox.Show("公司全名:" + cmp.FullName + " 已存在!");
                        return false;
                    }

                    pName = sess.CreateQuery("select Name from Company as p where p.Name='" + cmp.Name.Trim() + "'").List<string>();
                    if (pName.Count > 0)
                    {
                        MessageBox.Show("公司简称:" + cmp.Name + " 已存在!");
                        return false;
                    }

                    pName = sess.CreateQuery("select Number from Company as p where p.Number='" + cmp.Name.Trim() + "'").List<string>();
                    if (pName.Count > 0)
                    {
                        MessageBox.Show("公司代码:" + cmp.Number + " 已存在!");
                        return false;
                    }

                }
                return true;
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
                return false;
            }
        }
    }
}
